
close all
clear all
clc

DATFileName={
    'sph01.dat'; % 1: SPM on KCl at neutral pH
    'sph02.dat'; % 2: SPM on KCl/FeCl3 at pH 5.0  
    'sph01.dat'; % 3: SPM on KCl/FeCl3 at pH 4.0  Pressure=23 +/- 1.0
    'sph01.dat'; % 4: SPM on KCl/FeCl3 at pH 7.1  Pressure=30 
    'sph02.dat'; % 5: SPM on KCl/FeCl3 at pH 3.0
    'sph02.dat'; % 6: SPM on KCl/FeCl3 at pH 2.0  Press=
    'sph01.dat'; % 7: SPM on KCl/FeCl3 at pH 4.0 Short scan Press=29.7 mN/m
    'sph01.dat'; % 8: SPM on KCl/FeCl3 at pH 7.1 Short scan Press=
    }
MYTFileName={
    'sph01.dat.scan78.MYT';  % SPM on KCl at neutral pH
    'sph02.dat.scan115.MYT'; % SPM on KCl/FeCl3 at pH 5.0
    'sph01.dat.scan342.MYT'; % SPM on KCl/FeCl3 at pH 4.0 
    'sph01.dat.scan272.MYT'; % SPM on KCl/FeCl3 at pH 7.1
    'sph02.dat.scan238.MYT'; % SPM on KCl/FeCl3 at pH 3.0
    'sph02.dat.scan50.MYT';  % SPM on KCl/FeCl3 at pH 2.0
    'sph01.dat.scan362.MYT'; % SPM on KCl/FeCl3 at pH 4.0
    'sph01.dat.scan278.MYT'; % SPM on KCl/FeCl3 at pH 7.1
    }
MONScanNumber={
    78;   % SPM on KCl at neutral pH
    115;  % SPM on KCl/FeCl3 at pH 5.0
    342;  % SPM on KCl/FeCl3 at pH 4.0
    272;  % SPM on KCl/FeCl3 at pH 7.1
    238;  % SPM on KCl/FeCl3 at pH 3.0
    50;   % SPM on KCl/FeCl3 at pH 2.0
    362;  % SPM on KCl/FeCl3 at pH 4.0
    278;  % SPM on KCl/FeCl3 at pH 7.1
    }

GID_Qxy_File={
    'sph01_scan78_hline.txt';  % SPM on KCl at neutral pH
    'sph02_scan115_hline.txt'; % SPM on KCl/FeCl3 at pH 5.0
    'sph01_scan342_hline.txt'; % SPM on KCl/FeCl3 at pH 4.0 
    'sph01_scan272_hline.txt'; % SPM on KCl/FeCl3 at pH 7.1
    'sph02_scan238_hline.txt'; % SPM on KCl/FeCl3 at pH 3.0
    'sph02_scan50_hline.txt';  % SPM on KCl/FeCl3 at pH 2.0
    'sph01_scan362_hline.txt'; % SPM on KCl/FeCl3 at pH 4.0
    'sph01_scan278_hline.txt'; % SPM on KCl/FeCl3 at pH 7.1
    }
FileNum=1;

file =MYTFileName{FileNum};
% read i2
data=plcol(DATFileName{FileNum},MONScanNumber{FileNum},'i2','PRESSURE');
MON=data(:,1);
MON_err=sqrt(MON);
Press=data(:,2);

%% load data
[gid,x] = mythenread(file);
I0=1e4;
%I0=1;
pix = 1:size(gid,1);
gid_raw=gid;
gid_raw_err=sqrt(gid_raw);

gid_norm=zeros(size(gid_raw));

xx=x;
for k=1:length(xx)
    gid_norm(:,k)=I0*gid_raw(:,k)/MON(k);
end
stth=xx;
pixels=pix;
GID_norm=gid_norm;
imagesc(stth,pixels,GID_norm);
set(gca,'ydir','norm');
%% plot/home/beams11/9IDSTAFF/ameslab/9idc/aug2012/data/matlab_mythen
close all
s=ones(3,3)
gid_conv=conv2(gid_norm,s,'same')/9;
close 
imagesc(xx,pix,gid_conv)% %% read i2
%contour(xx,pix,gid_conv,[0.1:1:0.3]);
colorbar;
set(gca,'ydir','norm');
set(gca,'xlim',[9 15]);
xlabel('stth');
ylabel('MYTHEN')

E=13.474 ; % X-ray energy in keV
lambda=12.398/E; % X-ray wavelength
K0=2*pi/lambda; % X-ray wave-number

alpha=0.065; % incident angle alpha in degrees
beta=0.0056*pixels-0.3701; % beta in degrees  for Aug 2015 data

Qz=K0*(sind(alpha)+sind(beta));
Qxy=2*K0*sind(stth/2);
%%
close all
figure
subplot(1,2,1)
[HC,HC]=contourf(Qxy,Qz,gid_conv,[20:5:50]);
set(HC,'linestyle','none');
colorbar
set(gca,'LineWidth',3);
set(gca,'FontSize',30,'FontName','times');
set(gca,'xlim',[0.85 1.85]);
set(gca,'xtick',[0.9:0.2:1.8]);
axis square
%%
clc
E=13.474 ; % X-ray energy in keV
lambda=12.398/E; % X-ray wavelength
K0=2*pi/lambda; % X-ray wave-number

alpha=0.065; % incident angle alpha in degrees
beta=0.0056*pixels-0.3701; % beta in degrees  for Aug 2015 data

Qz=K0*(sind(alpha)+sind(beta));
QM=1;
QN=1;

%% Partition the Qz axis 

for k=1:length(Qz)
    if Qz(k)>0;QM=k;break;
    end
end

for k=1:length(Qz)
    if Qz(k)>0.2;QN=k-1;break;
    end
end

Qxy=2*K0*sind(stth/2);

Ixy=zeros(size(xx));
Ixy_norm=zeros(size(xx));
sIxy=zeros(size(xx));
sIxy_norm=zeros(size(xx));
for k=1:length(xx)
    Ixy(k)=sum(gid_raw(QM:QN,k),1);
    Ixy_norm(k)=Ixy(k)/MON(k);
    sIxy(k)=sqrt(Ixy(k));
    sIxy_norm(k)=Ixy(k)/MON(k)*sqrt(1/Ixy(k)+1/MON(k));
end

H=ploterr(Qxy,Ixy_norm,[],sIxy_norm,'ko','hhy',0.1);
set(H,'markersize',10,'LineWidth',3,'MarkerFaceColor','w');
Txt_Save(GID_Qxy_File{FileNum}, Qxy,Ixy_norm,sIxy_norm);









